package com.xinchen.all.ui.activity

import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener
import com.maple.base.base.BaseActivity
import com.maple.utils.util.LogUtils
import com.maple.utils.util.PermissionUtils
import com.maple.utils.util.RequestPermissionResult
import com.xinchen.all.R

class LocationActivity : BaseActivity() {

    private var tvLocation: TextView? = null
    private var btnLocation: Button? = null

    override fun getLayoutId(): Int = R.layout.activity_location

    //声明AMapLocationClient类对象
    private var aMapLocationClient: AMapLocationClient? = null
    //声明定位回调监听器
    private val aMapLocationListener: AMapLocationListener = object : AMapLocationListener {
        override fun onLocationChanged(aMapLocation: AMapLocation?) {
            if(aMapLocation != null) {
                if (aMapLocation.errorCode == 0) {
                    LogUtils.logGGQ("===aMapLocation===>>${aMapLocation.address}")
                    LogUtils.logGGQ("===aMapLocation===>>${aMapLocation.latitude}")
                    LogUtils.logGGQ("===aMapLocation===>>${aMapLocation.longitude}")
                    val address: String = "纬度：${aMapLocation.latitude} - 经度：${aMapLocation.latitude} -地址：${aMapLocation.address}"
                    tvLocation?.text = address
                }
            }
        }
    }

    private val aMapLocationClientOption: AMapLocationClientOption by lazy {
        AMapLocationClientOption().apply {
//            this.locationPurpose = AMapLocationClientOption.AMapLocationPurpose.Sport
//            this.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy
//            this.isOnceLocation = true
            this.isOnceLocationLatest = true
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        tvLocation = findViewById(R.id.tv_location)
        btnLocation = findViewById(R.id.btn_location)
        btnLocation?.setOnClickListener {
            tvLocation?.text = "正在获取位置..."
            requestLocationPermission()
        }
    }
    override fun initData(savedInstanceState: Bundle?) {
        requestLocationPermission()
    }

    private fun getLocation() {
        AMapLocationClient.updatePrivacyAgree(applicationContext, true)
        AMapLocationClient.updatePrivacyShow(applicationContext, true, true)

        aMapLocationClient = AMapLocationClient(applicationContext)
        aMapLocationClient?.let {
            it.setLocationListener(aMapLocationListener)
            it.setLocationOption(aMapLocationClientOption)
        }
        aMapLocationClient?.startLocation()
    }

    private fun requestLocationPermission() {
        LogUtils.logGGQ("申请权限")
        PermissionUtils.requestLocation(this,object :RequestPermissionResult{
            override fun onRequestPermissionSuccess() {
                LogUtils.logGGQ("权限通过")
                getLocation()
            }
        })
    }


    override fun onDestroy() {
        super.onDestroy()
        aMapLocationClient?.let {
            it.unRegisterLocationListener(aMapLocationListener)
            it.stopLocation()
        }
    }
}